package com.glwan.fixedassetapp.helper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.handler.ReaderResponseInterfaces;
import com.glwan.fixedassetapp.model.RFIDTagData;
import com.glwan.fixedassetapp.utils.CommonUtil;
import com.rscja.deviceapi.RFIDWithUHFUART;
import com.rscja.deviceapi.entity.Gen2Entity;
import com.rscja.deviceapi.entity.UHFTAGInfo;
import com.rscja.deviceapi.interfaces.IUHFInventoryCallback;
import com.zebra.rfid.api3.ENUM_TRANSPORT;
import com.zebra.rfid.api3.InvalidUsageException;
import com.zebra.rfid.api3.OperationFailureException;
import com.zebra.rfid.api3.ReaderDevice;
import com.zebra.rfid.api3.Readers;

import java.util.ArrayList;
import java.util.Arrays;

public class U530ReaderHelper{

    private static final String TAG = "U530";

    IntentFilter mScanResultIntentFilter;

    private static U530ReaderHelper u530ReaderHelper;

    public boolean loopFlag = false;

    private boolean inventoryStatus = false;


    private Activity activity;

    private Context context;

    private BroadcastReceiver scanResultReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String resultString = intent.getStringExtra("string");
            Log.d("TAG", "接收到扫码结果string:" + resultString);
        }
    };

    public static U530ReaderHelper getInstance(Activity activity) {
        if (u530ReaderHelper == null) {
            u530ReaderHelper = new U530ReaderHelper();
        }
        u530ReaderHelper.activity = activity;
        return u530ReaderHelper;
    }

    private void registerScanResultReceiver() {
        //注册接收扫码结果的广播
        //"com.uc.scanner.result"这个action值 对应 "扫描助手App"广播模式的广播action
        mScanResultIntentFilter = new IntentFilter("com.uc.scanner.result");
        context=this.activity;
        context.registerReceiver(scanResultReceiver, mScanResultIntentFilter);
    }

    private void unRegisterScanResultReceiver() {
        if(scanResultReceiver  != null) {
            context=this.activity;
            context.unregisterReceiver(scanResultReceiver);
        }
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                UHFTAGInfo info = (UHFTAGInfo) msg.obj;
                Log.i(TAG, "UHFReadTagFragment.info=" + info);
                String rssi = info.getRssi();
                String ant = info.getAnt();
                String tid = info.getTid();
                String epc = info.getEPC();
                String user = info.getUser();
                // System.out.println("------------------------------------读取数据开始---------------------------------------");
                // System.out.println("tid:" + tid);
                // System.out.println("epc:" + epc);
                // System.out.println("user:" + user);
                // System.out.println("------------------------------------读取数据结束---------------------------------------");
                Log.i(TAG, "UHFReadTagFragment.tid=" + tid + " epc=" + epc + " user=" + user);
                RFIDTagData rfidTagData = new RFIDTagData();
                // rfidTagData.setRssi(Integer.parseInt(rssi));
                rfidTagData.setEpc(epc);
                rfidTagData.setAntennaID(Short.parseShort(ant));
//                addDataToList(rfidTagData);
            } else if (msg.what == 2) {
                if (loopFlag) {
                    handler.sendEmptyMessageDelayed(2, 10);
                    // setTotalTime();
                } else {
                    handler.removeMessages(2);
                }
            }
        }
    };



    public boolean startInventory() {
        if (!inventoryStatus) {     // 识别标签
            registerScanResultReceiver();
        } else {
            stopInventory();
            CommonUtil.toastShow(activity, "停止盘读", CommonUtil.SUCCESS, true);
        }
        return false;
    }

    public boolean stopInventory() {
        Log.d(TAG, "停止盘读");
        if (loopFlag) {
            loopFlag = false;
            this.unRegisterScanResultReceiver();
        }
        return loopFlag;
    }



    /**
     * 添加数据到列表中
     *
     * @param
     */
//    private void addDataToList(RFIDTagData rfidTagData) {
//        new AsyncTask<Void, Void, Void>() {
//            @Override
//            protected Void doInBackground(Void... voids) {
//                activity.runOnUiThread(() -> {
//                    if (activity instanceof
//                            ReaderResponseInterfaces.ResponseTagHandler) {
//                        ((ReaderResponseInterfaces.ResponseTagHandler) activity).handleResponse(rfidTagData);
//                        // editText.append(stringBuilder.toString());
//                    }
//                });
//                return null;
//            }
//        }.execute();
//    }






}
